package org.bonorsoft.modules.control.controller;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.system.vo.Ztree;
import org.bonorsoft.modules.control.entity.Label;
import org.bonorsoft.modules.control.service.ILabelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 标签管理
 * @Author: WilliamYe
 * @Date: 2021-03-19
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/control/label")
public class LabelController extends BonorController<Label, ILabelService> {
    private String prefix = "control/label";

    @Autowired
    private ILabelService labelService;

    /**
     * 首页
     */
    @RequiresPermissions("control:label:view")
    @GetMapping()
    public String index() {
        return prefix + "/label";
    }

    /**
     * 标签管理-分页列表查询
     *
     * @param label
     * @param req
     * @return
     */
    @RequiresPermissions("control:label:list")
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Label> queryPageList(Label label, HttpServletRequest req) {
        QueryWrapper<Label> queryWrapper = QueryGenerator.initQueryWrapper(label, req.getParameterMap());
        queryWrapper.orderByAsc("order_num");
        return labelService.list(queryWrapper);
    }

    /**
     * 新增标签
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap) {
        Label label = null;
        if (0L != parentId) {
            label = labelService.getById(parentId);
        } else {
        	label = new Label().setLabelId(0L).setLabelName("根标签");
        }
        mmap.put("label",label);
        return prefix + "/add";
    }

    /**
     * 添加
     *
     * @param label
     * @return
     */
	@AutoLog(value = "标签-添加")
	@RequiresPermissions("control:label:add")
	@PostMapping(value = "/add")
	@ResponseBody
    public Result<?> add(@Validated Label label) {
		if (UserConstants.LABEL_NAME_NOT_UNIQUE.equals(checkLabelNameUnique(label))) {
			return Result.error("新增标签'" + label.getLabelName() + "'失败，标签已存在");
		}
		labelService.save(label);
		return Result.ok("添加成功！");
    }
	/**
	 * 校验标签名称
	 */
	@PostMapping("/checkLabelNameUnique")
	@ResponseBody
	public String checkLabelNameUnique(Label label) {
		return labelService.checkLabelNameUnique(label);
	}
	/**
	 * 选择标签树
	 */
	@GetMapping("/selectLabelTree/{labelId}")
	public String selectDeptTree(@PathVariable("labelId") Long labelId, ModelMap mmap) {
		mmap.put("label", labelService.getById(labelId));
		return prefix + "/tree";
	}

	/**
	 * 加载标签列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData() {
		return labelService.selectLabelTree();
	}


    /**
     * 编辑页面
     */
    @GetMapping("/edit/{labelId}")
    public String edit(@PathVariable("labelId") String labelId, ModelMap map) {
        map.put("label", labelService.getById(labelId));
        return prefix + "/edit";
    }

	/**
	 * 编辑
	 *
	 * @param label
	 * @return
	 */
	@AutoLog(value = "标签-编辑")
	@RequiresPermissions("control:label:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated Label label) {
		if (UserConstants.LABEL_NAME_NOT_UNIQUE.equals(checkLabelNameUnique(label))) {
			return Result.error("修改标签'" + label.getLabelName() + "'失败，标签名称已存在");
		} else if (label.getParentId().equals(label.getLabelId())) {
			return Result.error("修改标签'" + label.getLabelName() + "'失败，上级标签不能是自己");
		}
		labelService.updateLabel(label);
		return Result.ok("编辑成功!");
	}

	/**
	 * 删除菜单
	 */
	@AutoLog(value = "标签-删除", operateType = 4)
	@RequiresPermissions("control:label:remove")
	@DeleteMapping("/remove/{labelId}")
	@ResponseBody
	public Result<?> remove(@PathVariable("labelId") Long labelId) {
		if (labelService.selectCountLabelByParentId(labelId) > 0) {
			return Result.error("存在子标签,不允许删除");
		}
		labelService.removeById(labelId);
		return Result.ok("删除成功");
	}

//    /**
//     * 批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "标签管理-批量删除")
//    @RequiresPermissions("control:label:deleteBatch")
//    @DeleteMapping(value = "/deleteBatch")
//    @ResponseBody
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.labelService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.ok("批量删除成功！");
//    }
//
//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "标签管理-通过id查询")
//    @GetMapping(value = "/queryById")
//    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
//        Label label = labelService.getById(id);
//        return Result.ok(label);
//    }
//
//    /**
//     * 导出excel
//     *
//     * @param request
//     * @param label
//     */
//    @AutoLog(value = "标签管理-导出excel")
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, Label label) {
//        return super.exportXls(request, label, Label.class, "标签管理");
//    }
//
//    /**
//     * 通过excel导入数据
//     *
//     * @param request
//     * @param response
//     * @return
//     */
//    @AutoLog(value = "标签管理-通过excel导入数据")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    @ResponseBody
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, Label.class);
//    }
//
//    /**
//     * 模板下载
//     *
//     * @return
//     */
//    @AutoLog(value = "标签管理-模板下载", operateType = 6)
//    @RequestMapping(value = "/importTemplate")
//    public ModelAndView importTemplate() {
//        return super.importTemplateXls(Label.class, "标签管理");
//    }

}
